<?php
require('config.php');

        $user_id=$_REQUEST['user_id'];
        $dengji=$_REQUEST['dengji'];
        $sql="select * from `".$oto."_users` where userId='{$user_id}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$user_dq_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
        $sql="select * from `".$oto."_users`";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$user_zhong_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$sql="select * from `".$oto."_users` where partnerId='{$user_id}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$user_one_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(!isset($user_one_info))
		{
			$data[0]['all']=0;
			$data[0]['one']=0;
			$data[0]['two']=0;
			$data[0]['three']=0;
		}
		else
		{
			$data[0]['all']=count($user_one_info);
			$one=0;
			for($i=0;$i<count($user_one_info);$i++)
			{
				$num=0;
				$tc_money=0;
				$cid=$user_one_info[$i]['userId'];
				$sql="select * from `".$oto."_distribution_log` where cid='{$cid}' and level='1'";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$user_one_order_info[]=$row;//将取得的所有数据赋值给person_info数组
				}
				if(!isset($user_one_order_info)) ;
				else
				{
					for($j=0;$j<count($user_one_order_info);$j++)
					{
						$num++;
						$tc_money+=$user_one_order_info[$j]['gain_price'];
					}
				}
				$user_one_info[$i]['order_num']=$num;
				$user_one_info[$i]['order_tc_money']=$tc_money;
			}
			for($i=0;$i<count($user_zhong_info);$i++)
			{
				for($j=0;$j<count($user_one_info);$j++)
				{
					if($user_one_info[$j]['userId']==$user_zhong_info[$i]['partnerId'])
					{
						$user_two_info[$one]=$user_zhong_info[$i];
						$one++;
					} 
				}
			}
			$data[0]['one']=$one;
			if(!isset($user_two_info))
			{
				$data[0]['two']=0;
				$data[0]['three']=0;
			}
			else
			{
				$two=0;
				for($i=0;$i<count($user_two_info);$i++)
				{
					$num=0;
					$tc_money=0;
					$cid=$user_two_info[$i]['userId'];
					$sql="select * from `".$oto."_distribution_log` where cid='{$cid}' and level='2'";
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$user_two_order_info[]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(!isset($user_two_order_info)) ;
					else
					{
						for($j=0;$j<count($user_two_order_info);$j++)
						{
							$num++;
							$tc_money+=$user_two_order_info[$j]['gain_price'];
						}
					}
					$user_two_info[$i]['order_num']=$num;
					$user_two_info[$i]['order_tc_money']=$tc_money;
				}
				for($i=0;$i<count($user_zhong_info);$i++)
				{
					for($j=0;$j<count($user_two_info);$j++)
					{
						if($user_two_info[$j]['userId']==$user_zhong_info[$i]['partnerId'])
						{
							$user_three_info[$two]=$user_zhong_info[$i];
							$two++;
						} 
					}
				}
				if(isset($user_three_info))
				{
					for($i=0;$i<count($user_three_info);$i++)
					{
						$num=0;
						$tc_money=0;
						$cid=$user_three_info[$i]['userId'];
						$sql="select * from `".$oto."_distribution_log` where cid='{$cid}' and level='3'";
						$result=$db->query($sql);
						while($row=$result->fetch_assoc()){
							$user_three_order_info[]=$row;//将取得的所有数据赋值给person_info数组
						}
						if(!isset($user_three_order_info)) ;
						else
						{
							for($j=0;$j<count($user_three_order_info);$j++)
							{
								$num++;
								$tc_money+=$user_three_order_info[$j]['gain_price'];
							}
						}
						$user_three_info[$i]['order_num']=$num;
						$user_three_info[$i]['order_tc_money']=$tc_money;
					}
				}
				$data[0]['two']=$two;
				$data[0]['three']=count($user_three_info);
			}
		}
		$data[0]['ljxs']=$user_dq_info[0]['agentTotalPrice'];
		$data[0]['ljyj']=$user_dq_info[0]['agentTotalPrice'];
		$data[0]['my_yj']=$user_dq_info[0]['agentTotalPrice'];
		$data[0]['ytx_yj']=$user_dq_info[0]['agentPayPrice'];
        $data[0]['ktx_yj']=$user_dq_info[0]['agentBalance'];
		//echo json_encode($data);
		if($dengji==1 || $dengji=='1') echo json_encode($user_one_info);
		if($dengji==2 || $dengji=='2') echo json_encode($user_two_info);
		if($dengji==3 || $dengji=='3') echo json_encode($user_three_info);
?>